#
# mingw makefile generics
#

CC      = ${MINGWBASE}gcc
CXX     = ${MINGWBASE}g++
AR      = ${MINGWBASE}ar
OBJCOPY = ${MINGWBASE}objcopy
STRIP   = ${MINGWBASE}strip --strip-unneeded
WINDRES = ${MINGWBASE}windres
PEFIX   = ${pefix}

BINEXT  = .exe

DSOLDFLAGS = -shared
DSOPRE     =
DSOPOST    = .dll
DSORPATH   =
DSOSONAME  = -Wl,-soname,

# Don't use MSVCRT for stdio; it's slower and is not safe.
# At one point (circa 2009) the MinGW implementations actually
# caused performance issues, but this is no longer the case.
ARCH_CFLAGS   += -D__USE_MINGW_ANSI_STDIO
ARCH_CXXFLAGS += -D__USE_MINGW_ANSI_STDIO

# Fix MINGW's shared library fanaticism
ARCH_LDFLAGS += -static-libstdc++ -static-libgcc

# Thought that was enough? Nope, here's some vars for forcing mixed linking.
# Prefix these to the linking flags for each library that will be linked to
# MINGW builds. MINGW likes to link these however it wants otherwise.
LINK_STATIC_IF_MIXED  ?= -Wl,-Bstatic
LINK_DYNAMIC_IF_MIXED ?= -Wl,-Bdynamic

# OpenGL must be linked to MZX
core_ldflags += -lopengl32

# Windows needs PNG to be statically linked (inc. zlib)
LIBPNG_LDFLAGS = $(shell libpng-config --static --ldflags)

ifeq (${BUILD_LIBSDL2},)
SDL_DLL := SDL.dll
else
SDL_DLL := SDL2.dll
endif

ifdef DEBUG
ARCH_LDFLAGS += -mconsole
endif

pefix := arch/mingw/pefix${BINEXT}

-include arch/mingw/pefix.d

arch/mingw/pefix.o: arch/mingw/pefix.c
	$(if ${V},,@echo "  CC      " $<)
	${HOST_CC} -MD ${CFLAGS} -c $< -o $@

${pefix}: arch/mingw/pefix.o
	$(if ${V},,@echo "  LINK    " ${pefix})
	${HOST_CC} arch/mingw/pefix.o -o ${pefix}

all: ${pefix}

clean:
	${RM} -f ${pefix} arch/mingw/pefix.o arch/mingw/pefix.d

#
# Windows builds must copy $SDL_DLL
# For SDL 1.2 builds, we want directx.bat too
#

build: ${build}
	${CP} ${SDL_PREFIX}/bin/${SDL_DLL} ${build}
#	At one point this actually did something useful.
#	The SDL DLL will generally stay the same between releases,
#	so this is kind of pointless.
#	${PEFIX} ${build}/${SDL_DLL}
ifeq (${LIBSDL2},0)
	${CP} arch/mingw/directx.bat ${build}
endif
ifeq (${BUILD_UTILS},1)
	${CP} arch/mingw/checkres* ${build}/utils
endif
	@arch/manifest.sh ${build}

SUBARCH := ${shell echo ${SUBPLATFORM} | sed 's,windows-,,'}
include arch/zip.inc
